package project.controllers.workers.WorkerWithFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;

import project.model.Garage;
import project.model.IGarage;
import project.model.IMaster;
import project.model.IOrder;
import project.model.Master;
import project.model.Order;
import project.model.OrderStatus;
import textFileWorkerWrapper.TextFileWorkerWrapper;

class ReaderFromFile {

	private ArrayList<IGarage> garages = new ArrayList<IGarage>();
	private ArrayList<IMaster> masters = new ArrayList<IMaster>();
	private ArrayList<IOrder> orders = new ArrayList<IOrder>();

	private static final String PARSER = " ";
	private static final String DELETED = "DELETED";
	private static final String CANCELED = "CANCELED";
	private static final String MADE = "MADE";
	private static final String DATE_FORMAT = "dd.MM.yyyy";

	ArrayList<IMaster> getListMastersFromFile(TextFileWorkerWrapper masterFile) {
		if (masters.isEmpty()) {

			ArrayList<String> temp = new ArrayList<String>();
			temp = masterFile.readFromFile();
			String arr[] = new String[1];
			for (String s : temp) {
				arr = s.split(PARSER);
				IMaster master = new Master(arr[0]);
				masters.add(master);
			}
		}
		return masters;
	}

	ArrayList<IGarage> getListGaragesFromFile(TextFileWorkerWrapper garageFile) {
		if (garages.isEmpty()) {

			ArrayList<String> temp = new ArrayList<String>();
			temp = garageFile.readFromFile();
			String arr[] = new String[1];
			for (String s : temp) {
				arr = s.split(PARSER);
				IGarage garage = new Garage(Integer.parseInt(arr[0]));
				garages.add(garage);
			}
		}

		return garages;

	}

	ArrayList<IOrder> getOrderFromFile(TextFileWorkerWrapper orderFile)
			throws NumberFormatException, ParseException {
		if (orders.isEmpty()) {
			ArrayList<String> temp = new ArrayList<String>();
			HashMap<Integer, String> masT = new HashMap<Integer, String>();
			HashMap<Integer, Integer> garT = new HashMap<Integer, Integer>();
			temp = orderFile.readFromFile();
			String arr[] = new String[8];
			for (String s : temp) {
				if (s.equals("")) {
					continue;
				}
				arr = s.split(PARSER);
				IOrder order = new Order(Integer.parseInt(arr[0]),
						parser(arr[1]));
				order.setAllData(parser(arr[2]), parser(arr[3]),
						Integer.parseInt(arr[4]));
				if (arr[5].equals(CANCELED)) {
					order.setOrderStatus(OrderStatus.CANCELED);
				} else if (arr[5].equals(DELETED)) {
					order.setOrderStatus(OrderStatus.DELETED);
				} else if (arr[5].equals(MADE)) {
					order.setOrderStatus(OrderStatus.MADE);
				}
				masT.put(order.getNumberOrder(), arr[6]);
				garT.put(order.getNumberOrder(), Integer.parseInt(arr[7]));

				orders.add(order);
			}

			joinData(masT, garT);
		}

		return orders;

	}

	private GregorianCalendar parser(String h) throws ParseException {
		SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT);
		Date date = f.parse(h);

		GregorianCalendar date1 = (GregorianCalendar) Calendar.getInstance();
		date1.setTime(date);
		return date1;
	}

	private void joinData(HashMap<Integer, String> masterT,
			HashMap<Integer, Integer> garageT) {
		for (IOrder order : orders) {
			for (IMaster master : masters) {
				for (IGarage garage : garages) {
					if (masterT.get(order.getNumberOrder()).equals(
							master.getNameMaster())
							&& garageT.get(order.getNumberOrder()) == garage
									.getNumberGarage()) {
						order.sendMasterGarage(master, garage);
					}
				}
			}
		}
	}

}
